package com.li.utilstest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import kotlinx.android.synthetic.main.activity_image_browser.*

class ImageBrowserActivity : AppCompatActivity() {
    var currentPosition=1
    private val paths= arrayListOf<String>("https://c-ssl.duitang.com/uploads/item/202005/20/20200520024521_zkkfs.jpg","https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F0623%2F3e66d62bj00qv4dn1001lc000hs00c6c.jpg&thumbnail=650x2147483647&quality=80&type=jpg",
        "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F0623%2Ff8e6111aj00qv4dn1002bc000hs00q0c.jpg&thumbnail=650x2147483647&quality=80&type=jpg",
        "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F0623%2F2b9760a9j00qv4dn2002mc000hs00pyc.jpg&thumbnail=650x2147483647&quality=80&type=jpg")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_browser)
        initData()
    }

    private fun initData() {
        var adapter=ImageBrowserAdapter()
        adapter.setPaths(paths)
        img_vp.adapter=adapter
        setCurrent(1)
        img_vp.currentItem=1
        img_vp.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {}

            override fun onPageSelected(position: Int) {
                setCurrent(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        previous.setOnClickListener {
            if (currentPosition>0) currentPosition-- else currentPosition=paths.size-1
            img_vp.currentItem=currentPosition
        }
        next.setOnClickListener {
            if (currentPosition<paths.size-1) currentPosition++ else currentPosition=0
            img_vp.currentItem= currentPosition
        }
    }

    fun setCurrent(position:Int){
        currentPosition=position
        position_total.text="${currentPosition+1}/${paths.size}"
    }
}